#include "EndPointListWidget.h"
#include "mainWindow.h"

#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QMimeData>


EndPointListWidget::EndPointListWidget(QWidget *parent)
    : QListWidget(parent)
{
    setAcceptDrops(true);
    setDropIndicatorShown(true);
    setDragDropMode(QAbstractItemView::DropOnly);
}

EndPointListWidget::~EndPointListWidget()
{
}

void EndPointListWidget::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasFormat(MIME_TYPE)) {
        event->acceptProposedAction();
    }
}

void EndPointListWidget::dragMoveEvent(QDragMoveEvent *event)
{
    if (event->mimeData()->hasFormat(MIME_TYPE)) {
        event->acceptProposedAction();
    } else {
        event->ignore();
    }
}

void EndPointListWidget::dropEvent(QDropEvent *event)
{
    if (event->mimeData()->hasFormat(MIME_TYPE)) {
        QByteArray data = event->mimeData()->data(MIME_TYPE);
        QString portName = QString::fromUtf8(data);
        emit newPortAdded(portName, true);
        event->acceptProposedAction();
    }
}
